// 系统方法 微信小程序&支付宝小程序

const p = {
  getMenuButtonBoundingClientRect() {
    return wx.getMenuButtonBoundingClientRect()
  },
  showLoading(...args) {
    return wx.showLoading(...args)
  },
  hideLoading() {
    return wx.hideLoading(...args)
  },
  canIUse(...args) {
    return wx.canIUse(...args)
  },
  getSystemInfoSync() {
    return wx.getSystemInfoSync()
  },

  redirectTo(...args) {
    return wx.redirectTo(...args)
  },

  switchTab(...args) {
    return wx.switchTab(...args)
  },
  reLaunch(...args) {
    return wx.reLaunch(...args)
  },
  navigateTo(...args) {
    return wx.navigateTo(...args)
  },

  navigateBack(...args) {
    return wx.navigateBack(...args)
  },

  showModal(args) {
    return new Promise((resolve, reject) => {
      wx.showModal({
        cancelText: "取消",
        confirmText: "确定",
        ...args,
        success(res) {
          resolve(res)
        },
        fail(err) {
          reject(err)
        }
      })
    })
  },

  scanCode(args) {
    return new Promise((resolve, reject) => {
      wx.scanCode({
        ...args,
        success: (res) => {
          resolve(res)
        },
        fail(err) {
          reject(err)
        }
      })
    })
  },

  showToast(...args) {
    return wx.showToast(...args)
  },

  requestPayment(payInfo) {
    return new Promise((resolve, reject) => {
      wx.requestPayment({
        'appId': payInfo.appId,
        'timeStamp': payInfo.timeStamp + '',
        'nonceStr': payInfo.nonceStr,
        'package': payInfo.package,
        'signType': payInfo.signType,
        'paySign': payInfo.sign,
        success: function(res) {
          console.log("微信支付成功==", res)
          resolve(res)
        },
        fail: function(err) {
          console.log("微信支付失败==", err)
          reject(err)
        }
      })
    })
  },

  /** 订阅消息 */ 
  requestSubscribeMessage(tmplIds) {
    return new Promise((resolve, reject) => {
      // 订阅消息
      wx.requestSubscribeMessage({
        tmplIds: [...tmplIds],
        complete: (res) => {
          // console.log("订阅消息结果complete", res);
          // 目前就判断一个
          // 授权了
          if (res[tmplIds[0]] == "accept") {
            resolve(true)
            // console.log("授权了消息订阅");
          } else {
            resolve(false)
            // console.log("拒绝了消息订阅");
          }

        }
      })
    })
  },

  getStorageSync(...args) {
    return wx.getStorageSync(...args)
  },
  setStorageSync(...args) {
    return wx.setStorageSync(...args)
  },
  removeStorageSync(...args) {
    return wx.removeStorageSync(...args)
  },
  clearStorageSync() {
    return wx.clearStorageSync()
  },

  // 展示title加载图标
  showNavigationBarLoading() {
    return wx.showNavigationBarLoading()
  },
  // 隐藏title加载图标
  hideNavigationBarLoading() {
    return wx.hideNavigationBarLoading()
  },

  stopPullDownRefresh() {
    return wx.stopPullDownRefresh()
  },

  hideLoading() {
    return wx.hideLoading()
  },

  // 查询授权
  authorize(args) {
    return new Promise((resolve, reject) => {
      wx.authorize({
        ...args,
        success: () => {
          resolve()
        },
        fail: () => {
          reject()
        }
      })
    })
  },

  makePhoneCall(args) {
    return new Promise((resolve, reject) => {
      wx.makePhoneCall({
        ...args,
        success() {
          resolve()
        },
        fail() {
          reject()
        }
      })
    })
  },

  createAnimation(...args) {
    return wx.createAnimation(...args)
  },

  openLocation(...args) {
    return wx.openLocation(...args)
  },

  getLocation(args) {
    return new Promise((resolve, reject) => {
      wx.getLocation({
        ...args,
        success(res) {
          resolve(res)
        },
        fail(err) {
          reject(err)
        }
      })
    })
  },

  createMapContext(...args) {
    let mapContext = wx.createMapContext(...args)
    let _getCenterLocation = mapContext.getCenterLocation;

    mapContext.getCenterLocation = function(args) {
      return new Promise((resolve, reject) => {
        _getCenterLocation({
          ...args,
          success(res) {
            resolve(res)
          }
        })
      })
    }

    return mapContext
  },

  getUpdateManager() {
    return wx.getUpdateManager()
  },
}

try {
  wx.p = p
  p.appName = "微信"
  console.log("微信端");

} catch (error) {
  my.p = p
  p.appName = "支付宝"
  console.log("支付宝端");
}

export default p